package com.telaxo.shooter2012.gfx {
	import caurina.transitions.Tweener;

	import com.greensock.TweenLite;
	import com.telaxo.shooter2012.Engine;

	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class MenuState extends Sprite {
		public var stageRef : Stage;
		private var myText : TextField;
		private var myTextFormat : TextFormat;
		private var loadNext : Engine;

		public function MenuState() {
			myText = new TextField();
			myTextFormat = new TextFormat("Arial", 18, 0x99ff99);
			stageRef = stage;
			myText.defaultTextFormat = myTextFormat;
			myText.selectable = false;
			myText.autoSize = TextFieldAutoSize.LEFT;
			myText.filters = [new GlowFilter(0x00cc00)];
			myText.text = "Start";

			alpha = 0;

			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}

		private function init(e : Event) : void {
			removeEventListener(Event.ADDED_TO_STAGE, init);

			var titre : TextField = new TextField();
			titre.defaultTextFormat = new TextFormat("Arial", 42, 0x99ff99);
			titre.autoSize = TextFieldAutoSize.LEFT;
			titre.selectable = false;
			titre.filters = [new GlowFilter(0x00cc00, 1, 10, 10, 2, 3)];
			titre.text = "Space Invaders";
			addChild(titre);

			titre.x = stage.stageWidth / 2 - titre.width / 2;
			titre.y = stage.stageHeight / 2 - titre.height;

			TweenLite.to(this, 5, {alpha:1});

			var myTextContainer : Sprite = new Sprite();

			myTextContainer.addChild(myText);
			myTextContainer.mouseChildren = false;
			myTextContainer.buttonMode = true;

			myTextContainer.addEventListener(MouseEvent.MOUSE_DOWN, PlayPressed);

			myTextContainer.x = (stage.stageWidth - myTextContainer.width) / 2;
			myTextContainer.y = titre.y + titre.height + 20;

			addChild(myTextContainer);

			myTextContainer.addEventListener(MouseEvent.CLICK, PlayPressed);
		}

		public function remove() : void {
			addChild(new Engine(stageRef));

			stageRef.removeChild(this);

			if (loadNext != null)
				loadNext.load();
		}

		public function unload(loadMe : Engine = null) : void {
			if (loadMe != null)
				loadNext = loadMe;

			Tweener.addTween(this, {alpha:0, y:-400, time:2, onComplete:remove});
		}

		private function PlayPressed(e : MouseEvent) : void {
			Tweener.addTween(this, {alpha:0, time:2, onComplete:remove});
			new Engine(stageRef);
		}
	}
}  